<?php
// +----------------------------------------------------------------------
// | Workman and Gateway-worker
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2025 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace yicmf\worker;

use think\facade\App; 
use Workerman\Worker as WorkermanWorker;

/**
 * Worker控制器扩展类
 */
abstract class Server
{
    protected $worker;
    protected $socket   = '';
    protected $protocol = '';
    protected $host     = '';
    protected $port     = '';
    protected $option   = [];
    protected $context  = [];
    protected $event    = ['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerReload', 'onWebSocketConnect'];

    /**
     * 架构函数
     * @access public
     */
    public function __construct($option = [])
    {
        unset($option['handler']);
        unset($option['enable']);
        $this->option = array_merge($this->option,$option);
        if (empty($this->socket) && !empty($this->option['socket'])) {
            $this->socket = $this->option['socket'];
        }
        if (empty($this->protocol) && !empty($this->option['protocol'])) {
            $this->protocol = $this->option['protocol'];
        }
        if (empty($this->host) && !empty($this->option['host'])) {
            $this->host = $this->option['host'];
        }
        if (empty($this->port) && !empty($this->option['port'])) {
            $this->port = $this->option['port'];
        }
        // 实例化 Websocket 服务
        $this->worker = new WorkermanWorker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context);
        if (!empty($this->option['ssl'])) {
            $this->option['transport'] = 'ssl';
            unset($option['ssl']);
        }
        if (empty($this->option['pidFile'])) {
            $this->option['pidFile'] = root_path() . 'worker.pid';
        }
        // 设置应用根目录
        $this->worker->setRootPath(root_path());
        // 避免pid混乱
        $this->option['pidFile'] .= '_' . $this->port;
        $this->option['logFile'] = runtime_path(). 'server_'.$this->port.'_worker.log';

        // 设置服务器参数
        if (!empty($this->option)) {
           
            foreach ($this->option as $name => $val) {
                if ($val)
                {
                    if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {
                        WorkermanWorker::${$name} = $val;
                    } else {
                        $this->worker->$name = $val;
                    }
                }

            }
        }
    
      
        // 设置回调
        foreach ($this->event as $event) {
            if (method_exists($this, $event)) {
                $this->worker->$event = [$this, $event];
            }
        }

        // 初始化
        $this->init();
    }

    protected function init()
    { 
    }

//    public function start()
//    {
//        dump(123);
//        WorkermanWorker::runAll();
//    }

    public function __set($name, $value)
    {
        $this->worker->$name = $value;
    }

    public function __call($method, $args)
    {
        call_user_func_array([$this->worker, $method], $args);
    }
}
